package com.mingqijia.gassafety.job.service.aep;

import com.alibaba.fastjson.JSONObject;
import com.mingqijia.gassafety.shared.constant.UrlConst;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName EquipmentService.java
 * @Description
 * @createTime 2022年05月09日
 */
@Component
@Slf4j
public class EquipmentAepService {

    @Value(value = "${gassafety.aep.url}")
    public String targetUrl;


    public Map<String, Object> aepRequest(Map<String, Object> jsonData, int type) {
        log.info("EquipmentAepService——aepRequest,主要发送数据：{},type：{}",
                JSONObject.toJSONString(jsonData), type);
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = true;
        JSONObject jsonObject = null;
        try {
            String url = "";
            if (type == 1) {
                url = targetUrl + UrlConst.CREATE_EQUIPMENT.getUrl();
            } else if (type == 2) {
                url = targetUrl + UrlConst.REMOVE_EQUIPMENT.getUrl();
            }

            log.info("EquipmentAepService——url：{}", url);
            String jsonStr = JSONObject.toJSONString(jsonData);
            String body = HttpUtil.getBody(jsonStr, url, "");
            log.info("EquipmentAepService——aepRequest,响应结果body：{}",
                    JSONObject.toJSONString(body));
            jsonObject = JSONObject.parseObject(body);
            if (jsonObject == null) return resultMap;
            if (Integer.parseInt(jsonObject.get("code") + "") != 200) {
                result = false;
                resultMap.put("msg", jsonObject.get("msg"));
            }
            resultMap.put("data", jsonObject.get("data"));
        } catch (Exception e) {
            log.error("EquipmentAepService-error:{}", e);
            throw e;
        }
        resultMap.put("result", result);
        return resultMap;
    }

    /**
     * 
     * Description: 根据aep平台的deviceid去查询，不是imei <br> 
     *  
     * @author 王伟<br>
     * @taskId <br>
     * @param params
     * @param param
     * @return <br>
     */
    public Map<String, Object> get(Map<String, Object> params,String param) {
        log.info("EquipmentAepService——get,主要发送数据：{}",
                JSONObject.toJSONString(param));
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = true;
        JSONObject jsonObject = null;
        try {
            String url =targetUrl + UrlConst.QUERY_EQUIPMENT.getUrl()+param;
            String s = HttpUtil.get(params, url);
            log.info("EquipmentAepService——get,响应结果body：{}",
                    JSONObject.toJSONString(s));
            if (StringUtils.isEmpty(s)) {
                result=false;
                log.error("EquipmentAepService——get:{}",s);
            }else {
                jsonObject = JSONObject.parseObject(s);
                if (jsonObject.getInteger("code")==200) {
                    resultMap.put("data",jsonObject.getJSONObject("data"))  ;
                }
            }

        } catch (Exception e) {
            result=false;
        }
        resultMap.put("result",result);
        return resultMap;
    }
}